layui.use(['form'], () =>{
    var form = layui.form,
        $ = layui.$;

    let editType = sessionStorage.getItem('editType');
    sessionStorage.setItem('editType', null);
    let user = JSON.parse(sessionStorage.getItem('user'));
    if(editType == '0'){ //修改手机号
        $('#txt').text('手机号');
        if(user){
            $('#id').val(user.id);
            $('#dom').val(user.phone);
        }
    }else{
        $('#txt').text('邮箱');
        if(user){
            $('#id').val(user.id);
            $('#dom').val(user.email);
        }
    }
    //验证码获取
    $("#codeBtn").on("click", function (data) {
        let dom = $("#dom").val();
        if(user.email != dom){
            if(editType == '0'){
                util.mobileCode($, dom);
            }else util.emailCode($, dom);
        }else layer.msg('你尚未修改!');
    });
    //监听提交
    form.on('submit(submit)', function (data) {
        $.ajax({
            type: 'post',
            url: '/Class/login/codeVarify',//验证码验证
            dataType: 'json',
            data: { "testCode": data.field.code , "type": parseInt(editType) },
            success: (res) =>{
                if(res.code == 0){
                    $.ajax({
                        type: 'post',
                        url: '/Class/user/edit',//修改
                        dataType: 'json',
                        data: { "id": data.field.id, "value": data.field.dom, "type": parseInt(editType)},
                        success: (res) =>{
                            if(res.code == 0){
                                sessionStorage.setItem('user', JSON.stringify(res.data[0]));
                                util.closeLayer($, res);
                            }else layer.msg(res.msg);
                        }
                    })
                }else layer.msg(res.msg);
            }
        })
        return false;
    })
})